import time

# 未优化的代码
def unoptimized_loop(n):
    start_time = time.time()
    result = 0
    for i in range(n):
        result += i * (7 + 1) / 2  # 每次循环都重新计算 (7 + 1) / 2
    end_time = time.time()
    print(f"Unoptimized loop took {end_time - start_time:.6f} seconds")
    return result

# 优化后的代码
def optimized_loop(n):
    start_time = time.time()
    result = 0
    half_i_plus_1 = (7 + 1) / 2  # 提前计算 (7 + 1) / 2
    for i in range(n):
        result += i * half_i_plus_1
    end_time = time.time()
    print(f"Optimized loop took {end_time - start_time:.6f} seconds")
    return result

if __name__ == "__main__":
    n = 1000000
    unoptimized_loop(n)
    optimized_loop(n)